#pragma once
#include <string>

#include "json.hpp"
#include "unordered_map"

using json = nlohmann::json;

struct JsonResponse{
    std::string dataJson;
    bool success;
    std::string message;
};
struct RpcJsonParam
{
    json requestJson;
    JsonResponse response;
};



template <typename K, typename V, typename... Args>
std::unordered_map<K, V> make_map(K key, V value, Args... args) {
    std::unordered_map<K, V> result;
    result.emplace(std::move(key), std::move(value));
    
    if constexpr (sizeof...(args) > 0) {
        auto remaining = make_map<K, V>(std::forward<Args>(args)...);
        result.insert(remaining.begin(), remaining.end());
    }
    
    return result;
}

// 调整后的formatFromKV函数
template <typename K, typename V, typename... Args>
static std::string jsonFormatFromKV(K key, V value, Args... args) {
    // 直接使用make_map创建完整的unordered_map
    std::unordered_map<K, V> result = make_map<K, V>(
        std::move(key), std::move(value), 
        std::forward<Args>(args)...
    );
    
    json j = result;
    return j.dump();
}

class JsonService
{

public:
    

    virtual ~JsonService() = default;
    virtual void call(std::string, RpcJsonParam&) = 0;
};



inline void to_json(json& j, const JsonResponse& u) {
    j = json{{"dataJson", u.dataJson}, {"success", u.success}, {"message", u.message}};
}

inline void from_json(const json& j, JsonResponse& u) {
    j.at("dataJson").get_to(u.dataJson);
    j.at("success").get_to(u.success);
    j.at("message").get_to(u.message);
}

